//
//  WXPOrderVC.m
//  MexicoLoan
//
//  Created by wxp on 2025/2/25.
//

#import "WXPOrderVC.h"
#import "WXPOrderChannelView.h"
#import "WXPCommonAgreementView.h"
#import "WXPOrderListCell.h"
#import "WXPOrderDetailVC.h"

@interface WXPOrderVC ()

@property (nonatomic, strong) WXPNavView * navView;
@property (nonatomic, strong) WXPOrderChannelView * channelView;
@property (nonatomic, strong) UIView * blankPageView;
@property (nonatomic, strong) WXPCommonAgreementView * agreementView;
@property (nonatomic,   copy) NSString * type;

@end

@implementation WXPOrderVC

#pragma mark - lazy
- (WXPNavView *)navView{
    if (!_navView) {
        _navView = [WXPNavView new];
        _navView.leftTop = XY(0, 0);
        _navView.backBtn.hidden = true;
        [_navView resetViewWithTitle:@"Pedidos"];
        _navView.leftTop = XY(0, 0);
    }
    return _navView;
}
- (WXPOrderChannelView *)channelView{
    if (_channelView == nil) {
        _channelView = [WXPOrderChannelView  new];
        _channelView.centerXTop = XY(SCREEN_WIDTH/2, self.navView.bottom+10);
        WEAKSELF
        _channelView.blockChannelSwitch = ^(NSString *type) {
            weakSelf.type = type;
            [weakSelf refreshHeaderAll];
        };
    }
    return  _channelView;
}
- (WXPCommonAgreementView *) agreementView
{
    if (_agreementView == nil) {
        _agreementView = [WXPCommonAgreementView  new];
    }
    return  _agreementView;
}
- (UIView *)blankPageView{
    if (_blankPageView == nil) {
        _blankPageView = [UIView  new];
        _blankPageView.backgroundColor = [UIColor clearColor];
        _blankPageView.widthHeight = XY(SCREEN_WIDTH, 0);
        _blankPageView.leftTop = XY(0, 0);
        UIImageView * img = [UIImageView new];
        img.image = [UIImage imageNamed:@"icon-list-nodata"];
        img.widthHeight = XY(Ratio(241), Ratio(240));
        img.centerXTop = XY(_blankPageView.width/2, Ratio(60));
        [_blankPageView addSubview:img];
        UILabel * label = [UILabel new];
        [GlobalMethod setLabel:label widthLimit:0 numLines:0 lineSpace:5 isBold:false fontNum:12 textColor:COLOR_CONTENT aligent:NSTextAlignmentCenter text:@"No hay más contenido disponible en este\nmomento, continúe solicitando más préstamos." bgColor:[UIColor clearColor]];
        label.centerXTop = XY(img.centerX, img.bottom+Ratio(25));
        [_blankPageView addSubview:label];
        _blankPageView.height = label.bottom;
    }
    return  _blankPageView;
}

#pragma mark - viewDidLoad
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    // 打点
    [GlobalMethod hitPointWithPageName:KEY_LoanHistory action:KEY_Enter extInfo:nil];
    [self requestList];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.type = @"1";
    [self configUI];
    [self addRefresh];
}

- (void)configUI{
    [self.tableView registerClass:[WXPOrderListCell class] forCellReuseIdentifier:NSStringFromClass([WXPOrderListCell class])];
    [self.view addSubview:self.navView];
    [self.view addSubview:self.channelView];
    CGFloat bottom = 50+([GlobalMethod getBottomSafeAreaHeight] == 0 ? LEFT_MARDIN : [GlobalMethod getBottomSafeAreaHeight]);
    self.agreementView.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-bottom);
    [self.view addSubview:self.agreementView];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.viewBG.bounds;
    gradientLayer.colors = @[(id)COLOR_PRIMARY.CGColor, (id)COLOR_BACKGROUND.CGColor];
    gradientLayer.startPoint = CGPointMake(0.5, 0.0);
    gradientLayer.endPoint = CGPointMake(0.5, 1.0);
    [self.viewBG.layer insertSublayer:gradientLayer atIndex:0];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = COLOR_BACKGROUND;
    self.viewBG.backgroundColor = COLOR_BACKGROUND;
    self.tableHeaderTopBg.backgroundColor = [UIColor clearColor];
    self.tableView.height = self.agreementView.top-self.channelView.bottom-10;
    self.tableView.top = self.channelView.bottom+10;
    self.tableView.tableHeaderView = ^(void){
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake( 0, 0, SCREEN_WIDTH, LEFT_MARDIN)];
        return view;
    }();
}

#pragma mark - tabelView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.aryDatas.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WXPOrderListCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WXPOrderListCell class]) forIndexPath:indexPath];
    [cell resetCellWithModel:self.aryDatas[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [WXPOrderListCell getHeight:self.aryDatas[indexPath.row] className:@"" selectorName:@"resetCellWithModel:"];
}
//table header
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [NSObject clickFeedback];
    ModelOrder * model = self.aryDatas[indexPath.row];
    if(model.status.integerValue == 110 || model.status.integerValue == 169){
        return;
    }
    WXPOrderDetailVC * vc = [WXPOrderDetailVC new];
    vc.orderModel = model;
    [Global_Nav pushViewController:vc animated:true];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return isAry(self.aryDatas)?[UIView new]:self.blankPageView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return isAry(self.aryDatas)?CGFLOAT_MIN:self.blankPageView.height;
}
//table footer
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

#pragma mark - request
- (void)requestList{
    [RequestApi requestOrderListWithType:self.type pageSize:10 pageNum:self.pageNum delegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        if (self.isRemoveAll) {
            [self.aryDatas removeAllObjects];
        }
        NSArray * aryResponse = [GlobalMethod exchangeDic:response[KEY_orderRecordList] toAryWithModelName:@"ModelOrder"];
        if (aryResponse.count < 10) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        self.pageNum++;
        if (isAry(aryResponse)) {
            [self.aryDatas addObjectsFromArray: aryResponse];
        }else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}


@end
